package controllers

import (
	bc "translate_backend/controllers/BaseController"
	"translate_backend/db"
	"translate_backend/models"
	//"fmt"
	"github.com/gin-gonic/gin"
	//"github.com/gin-gonic/gin/binding"
	//"gopkg.in/mgo.v2/bson"
)

type ProjCon struct {
	bc.BaseController
	//services.Service
}

// 获取项目列表
func (con *ProjCon) GetProjList(c *gin.Context) {

	limitFields := map[string]int{"_id": 0}

	con.GetAllByPage("project", nil, limitFields, []string{"-CreateDate"}, c)

}

// 创建
func (con *ProjCon) Create(c *gin.Context) {

	var obj models.Project

	err := c.ShouldBindJSON(&obj)

	if err == nil {

		obj.SetupInitValues()

		res := db.InsertOne(obj, "project")
		if res {
			//创建对应表唯一索引
			var momgoSession = db.GetMgo()
			var dbname = db.DBName
			db.EnsureUniqueIndex(momgoSession, dbname, obj.ID, "langkey")

			con.ServeSuccess(obj, "", c)
		} else {
			con.ServeError(500, "", nil, c)
		}

	} else {
		//c.Data["json"] = err.Error()
		con.ServeError(403, "", nil, c)
	}
}

// 更新
func (con *ProjCon) Update(c *gin.Context) {
	con.UpdateObjectByID("project", c)
}

// 删除
func (con *ProjCon) Delete(c *gin.Context) {
	con.DeleteObjectByID("project", c)
	//TODO :

}
